package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.dao.WebAppMenuDao;
import ysq.xyg.web.model.dto.WebAppMenuDto;
import ysq.xyg.web.model.po.WebAppMenu;
import ysq.xyg.web.model.vo.WebAppMenuVo;
import ysq.xyg.web.service.WebAppMenuService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 业务模块-APP菜单-业务模块-APP菜单service实现
 *
 * @author congcong tao
 * @date 2019-10-29 10:20:29
 */
@Service("webAppMenuService")
public class WebAppMenuServiceImpl extends BaseServiceImpl<WebAppMenu> implements WebAppMenuService {

	@Resource
	private WebAppMenuDao webAppMenuDao;

	@Autowired
	public void setBaseDao(WebAppMenuDao webAppMenuDao) {
		this.baseDao = webAppMenuDao;
	}

	/**
	 * 分页查询
	 *
	 * @param webAppMenuVo 对象
	 * @param pageSize     展示数量
	 * @param pageNum      当前页面
	 * @return ReturnEntity 对象
	 * @author congcong tao
	 * @date 2019-10-29 10:20:29
	 */
	@Override
	public ReturnEntity page(WebAppMenuVo webAppMenuVo, int pageSize, int pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy(" sort asc, create_time desc");
		List<WebAppMenu> list = acceptList(webAppMenuVo);
		PageInfo<WebAppMenu> page = new PageInfo<WebAppMenu>(list);
		return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
	}

	/**
	 * 修改
	 *
	 * @param webAppMenuVo 对象
	 * @param currentUser  当前用户
	 * @return ReturnEntity 对象
	 * @author congcong tao
	 * @date 2019-10-29 10:20:29
	 */
	@Override
	@CacheEvict(value={"cacheWebAppMenu"}, allEntries=true)
	public ReturnEntity addOrUpdate(WebAppMenuVo webAppMenuVo, SysUser currentUser) {
		boolean isUpate = true;
		WebAppMenu webAppMenu;
		if (StrUtil.isEmpty(webAppMenuVo.getId())) {
			webAppMenu = new WebAppMenu(currentUser);
			isUpate = false;
		} else {
			webAppMenu = selectByPrimaryKey(webAppMenuVo.getId());
			if (ObjectUtil.isNull(webAppMenu)) {
				return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
			}
			webAppMenu.setUpdateTime(new Date());
			webAppMenu.setUpdateUser(currentUser.getId());
		}
		webAppMenu.setName(webAppMenuVo.getName());
		webAppMenu.setScopeType(webAppMenuVo.getScopeType());
		webAppMenu.setIcon(webAppMenuVo.getIcon());
		webAppMenu.setJumpUrl(webAppMenuVo.getJumpUrl());
		webAppMenu.setAppParams(webAppMenuVo.getAppParams());
		webAppMenu.setUrl(webAppMenuVo.getUrl());
		webAppMenu.setJumpType(webAppMenuVo.getJumpType());
		webAppMenu.setType(webAppMenuVo.getType());
		webAppMenu.setSort(webAppMenuVo.getSort());
		webAppMenu.setEditType(webAppMenuVo.getEditType());
		int num = 0;
		if (isUpate) {
			num = updateByPrimaryKey(webAppMenu);
		} else {
			num = insert(webAppMenu);
		}
		if (num <= 0) {
			return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
		}
		return new ReturnEntity();
	}

	/**
	 * 删除
	 *
	 * @param id          主键id
	 * @param currentUser 当前用户
	 * @return ReturnEntity 对象
	 * @author congcong tao
	 * @date 2019-10-29 10:20:29
	 */
	@Override
	@CacheEvict(value={"cacheWebAppMenu"}, allEntries=true)
	public ReturnEntity delete(String id, SysUser currentUser) {
		WebAppMenu webAppMenu = selectByPrimaryKey(id);
		if (webAppMenu != null) {
			webAppMenu.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
			webAppMenu.setUpdateTime(new Date());
			webAppMenu.setUpdateUser(currentUser.getId());
			int num = updateByPrimaryKey(webAppMenu);
			if (num > 0) {
				return new ReturnEntity();
			}
		}
		return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
	}

	/**
	 * 获取集合
	 *
	 * @param webAppMenuVo 对象
	 * @return List<BaseDict> BaseDict集合
	 * @author congcong tao
	 * @date 2019-10-29 10:20:29
	 */
	@Override
	public List<WebAppMenu> acceptList(WebAppMenuVo webAppMenuVo) {
		Example example = new Example(WebAppMenu.class);
		Example.Criteria criteria = example.createCriteria();
		if (StringUtil.isNotEmpty(webAppMenuVo.getRemoveTag())) {
			criteria.andEqualTo("removeTag", webAppMenuVo.getRemoveTag());
		}
		if (StringUtil.isNotEmpty(webAppMenuVo.getAccountId())) {
			criteria.andEqualTo("accountId", webAppMenuVo.getAccountId());
		}
		if (StringUtil.isNotEmpty(webAppMenuVo.getScopeType())) {
			criteria.andEqualTo("scopeType", webAppMenuVo.getScopeType());
		}
		if (StringUtil.isNotEmpty(webAppMenuVo.getName())) {
			criteria.andLike("name", "%" + webAppMenuVo.getName() + "%");
		}
		example.setOrderByClause(" sort asc, create_time desc");
		return webAppMenuDao.selectByExample(example);
	}

	@Override
	public List<WebAppMenuDto> selectEntityList(WebAppMenuVo webAppMenuVo) {
		return webAppMenuDao.selectEntityList(webAppMenuVo);
	}
}
